package com.smartboard.network.ui;

import android.os.AsyncTask;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

import com.smartboard.network.R;
import com.smartboard.network.client.GameClient;
import com.smartboard.network.game.AbstractGame;
import com.smartboard.network.game.IRoom;

/**
 * Created by cheas on 2018/8/23.
 */

public class CreateRoomDialog extends BaseDialog {

    private ProgressBar mProgressBar;
    private EditText mRoomNameText;
    private EditText mPasswordText;

    private CreateRoomTask mCreateRoomTask = null;
    private RoomActivity mActivity;

    public CreateRoomDialog(RoomActivity activity) {
        super(activity);

        mActivity = activity;
        setTitle(R.string.title_create_room);
        setContentView(R.layout.dialog_create_room);
        setCancelable(false);
        setCanceledOnTouchOutside(false);

        mRoomNameText = (EditText)findViewById(R.id.name);
        mPasswordText = (EditText)findViewById(R.id.password);
        mProgressBar = (ProgressBar)findViewById(R.id.progress);

        Button okBtn = (Button)findViewById(R.id.okBtn);
        Button cancelBtn = (Button)findViewById(R.id.cancenBtn);
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mCreateRoomTask != null)
                    return;
                String roomName = mRoomNameText.getText().toString();
                String password = mPasswordText.getText().toString();
                if(roomName.isEmpty()) {
                    mRoomNameText.setError(getContext().getString(R.string.error_field_required));
                    return;
                }
                MyApplication.hideKeyboard(mActivity);
                mCreateRoomTask = new CreateRoomTask(roomName, password);
                mCreateRoomTask.execute();
                showProgress(true);
            }
        });

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mCreateRoomTask != null)
                    return;
                dismiss();
            }
        });
        adjustWindowSize();
    }

    private void showProgress(boolean show) {
        if(show) {
            mProgressBar.setVisibility(View.VISIBLE);

        } else {
            mProgressBar.setVisibility(View.GONE);
        }
    }

    private class CreateRoomTask extends AsyncTask<Void, Void, IRoom> {
        String roomName;
        String password;

        public CreateRoomTask(String roomName, String password) {
            this.roomName = roomName;
            this.password = password;
        }

        @Override
        protected IRoom doInBackground(Void... params) {
            return GameClient.inst().createRoom(roomName, password.isEmpty() ? null : password);
        }

        @Override
        protected void onPostExecute(IRoom room) {
            mCreateRoomTask = null;
            showProgress(false);

            switch (GameClient.inst().getErrorCode()) {
                case AbstractGame.ERROR_NONE:
                    showToast(R.string.create_success);
                    dismiss();
                    break;
                case AbstractGame.ERROR_INVALID_PASSWORD:
                    showToast(R.string.error_incorrect_password);
                    mPasswordText.setError(getContext().getString(R.string.error_incorrect_password));
                    mPasswordText.requestFocus();
                    break;
                case AbstractGame.ERROR_NAME_EXIST:
                    showToast(R.string.create_fail_name_exist);
                    mRoomNameText.setError(getContext().getString(R.string.create_fail_name_exist));
                    mRoomNameText.requestFocus();
                    break;
                case AbstractGame.ERROR_TIMEOUT:
                    showToast(R.string.create_fail_nework);
                    break;
                default:
                    break;
            }
        }

        @Override
        protected void onCancelled() {
            mCreateRoomTask = null;
            showProgress(false);
        }
    }
}
